﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;

namespace CNCSoft.ErpApps.TMS.longACSS
{
    public partial class frmBookPhong : Form
    {
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();
        frmBookPhongDialog frm = null;

        public frmBookPhong()
        {
            InitializeComponent();
        }

        private void frmBookPhong_Load(object sender, EventArgs e)
        {
            LoadControl();
        }

        private void btn_SearchRoom_Click(object sender, EventArgs e)
        {
            try
            {
                if (searchLookUpEdit_KhoaHoc.EditValue != null)
                {
                    LoadKhoaHocByMaKhoaHoc(searchLookUpEdit_KhoaHoc.EditValue.ToString());
                    DataTable dt = ds.Tables["TableKhoaHocByMaKhoaHoc"];
                    LoadRoomSatisfyClass(dt.Rows[0]["MaNgayTrongTuan"].ToString(), dt.Rows[0]["MaTimeSlot"].ToString(), 
                                        Convert.ToDateTime(dt.Rows[0]["NgayBatDau"]), Convert.ToDateTime(dt.Rows[0]["NgayKetThuc"]));
                    gridControl_Data.DataSource = null;
                    gridControl_Data.DataSource = ds.Tables["TableFilterRoom"];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_AllRoom_Click(object sender, EventArgs e)
        {
            LoadControl();
        }

        private void gridView_Data_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            try
            {
                if (e.Column.Name == "gridColumn_DatPhong")
                {
                    if (searchLookUpEdit_KhoaHoc.EditValue != null)
                    {
                        DataRow dr = gridView_Data.GetDataRow(e.RowHandle);
                        frm = new frmBookPhongDialog(dr["MaPhong"].ToString(), searchLookUpEdit_KhoaHoc.EditValue.ToString(), true, false);
                        frm.ShowDialog();
                        frm.Close();
                    }
                    else
                    {
                        MessageBox.Show("Bạn chưa chọn lớp!");
                    }
                }
                else if (e.Column.Name == "gridColumn_TraPhong")
                {
                    DataRow dr = gridView_Data.GetDataRow(e.RowHandle);
                    frm = new frmBookPhongDialog(dr["MaPhong"].ToString(), dr["MaLop"].ToString(), false, true);
                    frm.ShowDialog();
                    frm.Close();
                }
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region "Coding"
        private void LoadControl()
        {
            LoadData();
        }

        private void LoadData()
        {
            try
            {
                LoadKhoaHoc();
                searchLookUpEdit_KhoaHoc.Properties.DataSource = ds.Tables["TableKhoaHoc"];
                LoadAllRoom();
                gridControl_Data.DataSource = null;
                gridControl_Data.DataSource = ds.Tables["TableAllRoom"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadKhoaHoc()
        {
            try
            {
                if (ds.Tables["TableKhoaHoc"] != null)
                    ds.Tables["TableKhoaHoc"].Clear();
                string sql = @"select dmkh.MaKhoaHoc, dmkh.TenKhoaHoc, dmkh.NgayBatDau, dmkh.NgayKetThuc, dmkh.MaNgayTrongTuan, dmkh.MaTimeSlot,
                                (dmkh.TenKhoaHoc + ', ' + dmtkb.TenNgayTrongTuan + ', ' + dmgh.TenGiohoc) as LichHoc, dmkh.TrangThai
                                from DM_KHOAHOC dmkh left outer join DM_NGAYTRONGTUAN dmtkb on dmtkb.MaNgayTrongTuan = dmkh.MaNgayTrongTuan
                                left outer join DM_GIOHOC dmgh on dmgh.MaGiohoc = dmkh.MaTimeSlot";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableKhoaHoc");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadKhoaHocByMaKhoaHoc(string makhoahoc)
        {
            try
            {
                if (ds.Tables["TableKhoaHocByMaKhoaHoc"] != null)
                    ds.Tables["TableKhoaHocByMaKhoaHoc"].Clear();
                string sql = @"select * from DM_KHOAHOC where MaKhoaHoc = '" + makhoahoc + @"'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableKhoaHocByMaKhoaHoc");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadAllRoom()
        {
            try
            {
                if (ds.Tables["TableAllRoom"] != null)
                    ds.Tables["TableAllRoom"].Clear();
                string sql = @"usp_AllRoomWithDetailInfo";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableAllRoom");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadRoomSatisfyClass(string mangaytrongtuan, string matimeslot, DateTime ngaybatdau, DateTime ngayketthuc)
        {
            try
            {
                if (ds.Tables["TableFilterRoom"] != null)
                    ds.Tables["TableFilterRoom"].Clear();
                string sql = @"usp_AllRoomSatisfyClass @mangaytrongtuan = '" +mangaytrongtuan+ @"', 
                                @matimeslot = '" +matimeslot+ @"', @ngaybatdau = '" +ngaybatdau+ @"', 
                                @ngayketthuc = '" +ngayketthuc+ @"'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableFilterRoom");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
